package
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import mx.containers.TabNavigator;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.buttons.SegmentedControl;
	import qnx.ui.core.Container;
	import qnx.ui.core.ContainerAlign;
	import qnx.ui.core.ContainerFlow;
	import qnx.ui.core.Containment;
	import qnx.ui.core.SizeMode;
	import qnx.ui.core.SizeUnit;
	import qnx.ui.core.Spacer;
	import qnx.ui.core.UIComponent;
	import qnx.ui.data.DataProvider;
	import qnx.ui.events.ListEvent;
	import qnx.ui.listClasses.*;
	import qnx.ui.text.Label;
	import qnx.ui.text.TextInput;
	import qnx.views.NavController;
	
	[SWF(height="600", width="1024", frameRate="30", backgroundColor="#FFFFFF")]
	
	// A simple container layout example //
	
	public class PBTest extends MovieClip
	{
		//the containers // 
		private var myMain:Container;
		private var mySub:Container;
		private var mySubRight:Container;
		private var mySubBottom:Container;
		
		private var base:Container, top:Container;
		private var containers:Array = new Array;
		private var tabBar:List;
		private var tabContent:Container;
		private var tabScrollPane:ScrollPane;
		
		private var navController:NavController;
		
		private var tabContentDict:Dictionary = new Dictionary();
		private var buttonArray:Array=[];
		//text input for second sub container //
		private var ti_msg:TextInput;
		
		//the back and next buttons //
		private var bt_snd:LabelButton;
		private var bt_conn:LabelButton;
		private var bt_sett:LabelButton;
		
		private var longText:Label;
		
		public function PBTest()
		{
			
			// create main container //
			myMain = new Container();
			myMain.margins = Vector.<Number>([0,0,0,0]);
			myMain.flow = ContainerFlow.HORIZONTAL;
			myMain.debugColor = 0xFFCC00;
			addChild(myMain);
			
			// create subcontainer on left side of the screen //
			mySub = new Container();
			mySub.margins = Vector.<Number>([2,2,2,2]);            
			mySub.size = 100;            
			mySub.debugColor = 0xFF3300;
			mySub.sizeUnit = SizeUnit.PERCENT;
			mySub.flow = ContainerFlow.VERTICAL;
			mySub.align = ContainerAlign.NEAR;
			mySub.padding = 10;
			
			//createTab();
			for(var j=0; j<5; j++){
				//var scrlPane:ScrollPane = createTabContent();
				var lbl:Label = new Label();
				lbl.text = "channel"+j;
				buttonArray.push({label:"channel"+j, view:lbl});
			}
			/*
			for(var i=1; i<buttonArray.length; i++){
				var contentlbl:Label = new Label();
				contentlbl.text = "blah " + i;
				tabContentDict[buttonArray[i]] = contentlbl;
			}*/
			
			// create a segemented control
			tabBar = new List();

			tabBar.height = 50;
			tabBar.width = 1024;
			tabBar.columnWidth = 150;
			tabBar.scrollBarAlpha = 0;
			tabBar.allowDeselect = false;
			tabBar.selectionMode = ListSelectionMode.SINGLE;
			tabBar.scrollDirection = ScrollDirection.HORIZONTAL;
			tabBar.containment = Containment.DOCK_TOP
			tabBar.addEventListener( ListEvent.ITEM_CLICKED, onSelect );
			tabBar.size = tabBar.height;
			tabBar.name = "tabBar";
			tabBar.dataProvider = new DataProvider( buttonArray );
			tabBar.selectedIndex = 0;
			mySub.addChild(tabBar);
			
			/*
			tabContent = new Container();
			tabContent.debugColor = 0xff0000;
			tabContent.margins = Vector.<Number>([10,10,10,10]);
			tabContent.size = 50;
			tabContent.sizeUnit = SizeUnit.PERCENT;
			tabContent.containment = Containment.CONTAINED;
			*/
			navController = new NavController();
			navController.container = mySub;
			navController.dataProvider = buttonArray;
			
			var lbl:Label = new Label();
			lbl.text = "test, test";
			mySub.addChild(lbl);
			//navController.changeSection(ButtonContainer);
			//mySub.addChild(tabContent);
			
			

			
			// create subcontainer as the bottom frame //
			mySubBottom = new Container();
			mySubBottom.margins = Vector.<Number>([5,5,5,5]);            
			mySubBottom.debugColor = 0x33FF33;
			mySubBottom.size = 12;
			mySubBottom.sizeUnit = SizeUnit.PERCENT;
			mySubBottom.flow = ContainerFlow.HORIZONTAL;
			mySubBottom.align = ContainerAlign.FAR;
			mySubBottom.containment = Containment.DOCK_BOTTOM; 
			
			// create and add UI components to the left container//            
			var labelFormat:TextFormat = new TextFormat();
			labelFormat = new TextFormat();            
			labelFormat.size = 22;

			// create and add back and next buttons //
			bt_snd = new LabelButton();
			bt_snd.label = "Send";
			bt_snd.size = 90;
			bt_snd.addEventListener(MouseEvent.CLICK, on_bt_snd);
			
			bt_conn = new LabelButton();
			bt_conn.label = "Connect";
			bt_conn.size = 90;
			
			bt_sett = new LabelButton();
			bt_sett.label = "Settings";
			bt_sett.size = 90;
			
			ti_msg = new TextInput();
			ti_msg.width = 700;
			
			
			mySubBottom.addChild(ti_msg); 
			mySubBottom.addChild(bt_snd);
			mySubBottom.addChild(bt_conn);
			mySubBottom.addChild(bt_sett);
			
			// add subcontainers to main container //
			myMain.addChild(mySub);   
			myMain.addChild(mySubBottom);
			
			// call setSize //
			myMain.setSize(stage.stageWidth, stage.stageHeight);
		}

		private function createTabContent():ScrollPane{
			var longText:Label = new Label();
			longText.wordWrap = true;
			longText.autoSize = TextFieldAutoSize.LEFT;
			longText.multiline = true;
			//longText.size = 200;
			longText.width = 900;
			
			longText.htmlText = "　　阿里山小火车翻覆意外，经现场勘查怀疑森氏栎是白蚁与真菌蛀食枝干，导致枝干断裂击中小火车。图片来源：台湾《联合报》\
				www.6park.com<br>\
				　　据台湾《联合报》报道，台湾嘉义阿里山小火车翻覆意外，检察官28日会同专家三度勘验现场，排除人为疏失及机械故障，竟是白蚁与真菌蛀食枝干，夺走五条人命且造成一百十三人轻重伤，确切原因仍待报告出炉。 www.6park.com\
				　　台湾嘉义地检署主任检察官林文亮会同铁路专家、森林系学者勘查现场，讯问驾驶员、列车长，初步确认是树干掉落撞到小火车，掉落原因指向白蚁与真菌入侵。 www.6park.com\
				　　到场的台大森林系教授王亚男、中兴大学森林系教授欧辰雄、曾彦学、王升阳等勘查后，认定断裂树种不是青刚栎，而是与青刚栎同属“壳斗科”的森氏栎。森氏栎属杂木，较一般木材重硬，肇祸的这株森氏栎，树龄逾百年。 www.6park.com\
				　　台大教授王亚男指出，这棵树在幼龄时就断了，再萌芽又新生出一个主干，两主干交叉处未完全愈合。他说，检视这株森氏栎在六米两主干交叉处，出现直径约一百厘米的腐朽空洞，是被白蚁蛀空，“腐朽加白蚁，又因为生长应力的消除，就掉下来了。” www.6park.com\
				　　王亚男说，树木含水率降到一定程度，生长应力迅速消失，变脆易断，加上后续长出的新生枝叶茂密，可能因此增加好几吨重量，而瞬间断裂，“这类意外人力无法检测及预防，属自然现象，没有人为疏失。” www.6park.com\
				　　检警综合现场状况及专家意见，勾勒出这件事故的大致轮廓是，树干掉落时，先斜向打中第七节车厢，驾驶员立即煞车，但小火车仍缓慢行进，掉落树干再撞击第八节车厢造成车厢倾斜，同时因茂盛枝条紧缠车厢，使小火车从第八节车厢开始连环翻覆。<br>\
				　　台检方表示，据行车记录器，事发时小火车时速十九公里，未逾二十公里速限，各车厢煞车系统均启动，估八节车厢载客一百五十至两百人，未超载，暂排除人为疏失及机械故障。 www.6park.com";
			
			
			
			var tabScrollPane:ScrollPane = new ScrollPane();
			tabScrollPane.width = 900;
			tabScrollPane.height = 450;
			tabScrollPane.x = 0;
			//tabScrollPane.y = 100;
			tabScrollPane.setScrollContent(longText);
			tabScrollPane.scrollY = longText.textHeight;
			return tabScrollPane;
		}
		private function on_bt_snd(e:Event):void{
			longText.htmlText += ti_msg.text + "<br>";
			tabScrollPane.scrollY = longText.textHeight;
			tabScrollPane.update();
			removeTab();
		}
		private function removeTab():void{
			tabBar.dataProvider.removeItemAt(tabBar.selectedIndex);
		}
		private function onSelect(event:ListEvent):void 
		{
			var item:Object = event.data;
			var view:Class = item.view as Class;
			if( tabBar.selectedIndex >=0 && tabBar.selectedIndex != navController.selectedIndex )
			{
				navController.changeSection( view );
			}
			trace(view);
			ti_msg.text = "blah" + tabBar.selectedIndex;
		}		
	}
}